package com.example.designmodel;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

public class StatusActivity extends AppCompatActivity {
    public static void launch(Context context) {
        Intent intent = new Intent(context, StatusActivity.class);
        context.startActivity(intent);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_status);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        status();
    }

    private void status(){
        StatusManager manager = new StatusManager();
        manager.setStatus(StatusManager.status1);
        manager.method2();
        manager.method1();
    }

    public static abstract class Status{
        //定义一个状态管理角色 ，提供子类访问
        protected StatusManager manager;

        public void setManager(StatusManager manager){
            this.manager = manager;
        }

        //行为1
        abstract void methord1();
        //行为2
        abstract void methodr2();
    }

    public static class Concrete1Status extends Status{


        @Override
        void methord1() {
            //本状态下必须处理的逻辑
            Log.i("test","handler1");
        }

        @Override
        void methodr2() {
            super.manager.setStatus(StatusManager.status2);
            super.manager.method2();
        }
    }

    public static class Concrete2Status extends Status{
        @Override
        void methord1() {
            super.manager.setStatus(StatusManager.status1);
            super.manager.method1();
        }

        @Override
        void methodr2() {
            //本状态下必须处理的逻辑
            Log.i("test","handler2");
        }
    }

    public static class StatusManager{
        public static final Concrete1Status status1 = new Concrete1Status();
        public static final Concrete2Status status2 = new Concrete2Status();

        private Status status;

        public void setStatus(Status status){
            this.status = status;
            //把当前的环境通知到各个实现类中
            this.status.setManager(this);
        }

        public void method1(){
            status.methord1();
        }

        public void method2(){
            status.methodr2();
        }


    }

}
